package org.hqs.doctor.controller;

import org.hqs.doctor.common.JsonResult;
import org.hqs.doctor.pojo.Doctor;
import org.hqs.doctor.service.DoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/doctor")
public class DoctorController {
    @Autowired
    private DoctorService doctorService;

    @GetMapping("/{orderId}")
    public JsonResult<Doctor> queryDoctorById(@PathVariable("orderId") Long orderId) {

        return new JsonResult(doctorService.queryDoctorById(orderId));
    }

    @GetMapping("/all")
    public JsonResult<List<Doctor>> findAllDoctors() {
        return new JsonResult<>(doctorService.findAllDoctors());
    }

    @PostMapping("/add")
    public JsonResult addDoctor(@RequestBody Doctor doctor) {
        int result = doctorService.addDoctor(doctor);
        if (result > 0) {
            return new JsonResult("200", "添加医生成功");
        } else {
            return new JsonResult( "400", "添加医生失败");
        }
    }

    @PutMapping("/update")
    public JsonResult updateDoctor(@RequestBody Doctor doctor) {
        int result = doctorService.updateDoctor(doctor);
        if (result > 0) {
            return new JsonResult( "200", "更新医生信息成功");
        } else {
            return new JsonResult( "400", "更新医生信息失败");
        }
    }

    @DeleteMapping("/delete")
    public JsonResult deleteDoctor(@RequestBody Doctor doctor) {
        Long id = (long) doctor.getId();
        if (id == null) {
            return new JsonResult("400", "未提供医生ID");
        }

        int result = doctorService.deleteDoctor(id);
        if (result > 0) {
            return new JsonResult("200", "删除医生成功");
        } else {
            return new JsonResult("400", "删除医生失败");
        }
    }

}
